<?php namespace app\service\controller;
use think\Controller;
use think\Db;

/**
 * 操作记录
 */
class OperLog extends Controller{
	
	private $modelObj;				//模型句柄
	public function __construct(){
		parent::__construct();
		if(empty($this -> modelObj)) $this -> modelObj = new \app\model\model\OperLog;
	}
	
	/**
	 * @param array $data :	数据
	 * @param string $fileName :  日志文件名称
	 * @return array 状态码
	 */
	public function logs($data = []){
		if( !empty($data) ){
			$os = strlen($_SERVER['HTTP_USER_AGENT']) > 255 ? substr($_SERVER['HTTP_USER_AGENT'], 0, 255) : $_SERVER['HTTP_USER_AGENT'];
			$log = [];
			foreach ($data['json'] as $k => $v) {
				$log[$k]['title'] = isset($data['title']) ? $data['title'] : '';
				$log[$k]['url'] = $_SERVER['REQUEST_URI'];
				$log[$k]['request_method'] = $_SERVER['REQUEST_METHOD'];
				$log[$k]['business_type'] = isset($data['business_type']) ? $data['business_type'] : 0;
				$log[$k]['oper_name'] = isset($data['oper_name']) ? $data['oper_name'] : '';
				$log[$k]['oper_user_id'] = isset($data['oper_user_id']) ? $data['oper_user_id'] : 0;
				$log[$k]['ip'] = getIP();
				$log[$k]['param'] = json_encode(input());
				$log[$k]['json_result'] = isset($v['json_result']) ? json_encode($v['json_result'], JSON_UNESCAPED_UNICODE) : '';
				$log[$k]['old_data'] = isset($v['old_data']) ? json_encode($v['old_data'], JSON_UNESCAPED_UNICODE) : '';
				$log[$k]['create_time'] = date("Y-m-d H:i:s", time());
				$log[$k]['oper_time'] = microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
				$log[$k]['os'] = $os;
				$log[$k]['status'] = 1;
			}
			$re = $this -> modelObj -> addAll($log);
		}
		return TRUE;
	}
	
}

?>